package sein.android.hopenote;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import sein.android.hopenote.Const.HopeNoteDb;
import sein.android.hopenote.Note_Search.SearchAdapter;
import sein.android.hopenote.Note_Search.SearchData;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Note_Add  extends Activity implements OnClickListener{
	
	public static DBAdapter HnopenHelper;
	public static SQLiteDatabase sdb;
	//public static Spinner spin_item;
	public static Button bt_save;
	//public static EditText et_value;
	public static TextView tv_title, tv_group, tv_unit;
	public static String str_group, str_date, str_item, qWhere;
	public static	ArrayList<String> lst_item, lst_value;
	public static	ArrayAdapter<String> adapter;
	public static Cursor cursor, cursor_item;
	public static ListView lv_item;
	private	static ArrayList<ItemData> Item_arr;
	private static ItemAdapter I_adapter;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.note_add);
		
		HnopenHelper = new DBAdapter(this);
        sdb = HnopenHelper.getReadableDatabase();
        str_group = HopeNoteDb.CURRENT_GROUP;
        str_item = HopeNoteDb.CURRENT_ITEM;
        tv_group.setText(str_group);
        
        Intent intent = getIntent();
        str_date = intent.getStringExtra(HopeNoteDb.EXTRA_NOTE_ADD_DATE);
        // DB
        if(str_date != null)
        {

            bt_save = (Button)findViewById(R.id.bt_save);
            tv_title = (TextView)findViewById(R.id.tv_title);
            tv_group = (TextView)findViewById(R.id.tv_group);
            lv_item = (ListView)findViewById(R.id.lv_item);
            
        	tv_title.setText("기록 수정");

        	qWhere = HopeNoteDb.COL_ITEM + " = '" + str_item + "'";
        	cursor_item = sdb.query(HopeNoteDb.TB_ITEM, new String[] {HopeNoteDb.COL_ITEM,HopeNoteDb.COL_CNT,HopeNoteDb.COL_UNIT}, qWhere, null, null, null, null);
        	qWhere = HopeNoteDb.COL_DATE + " = '" + str_date + "'";
        	cursor = sdb.query(HopeNoteDb.TB_NOTE, new String[] {HopeNoteDb.COL_ITEM,HopeNoteDb.COL_DOCUMENT1,HopeNoteDb.COL_DOCUMENT2}, qWhere, null, null, null, null);
        
        	cursor_item.moveToFirst();
	        cursor.moveToFirst();
	        Item_arr = new ArrayList<ItemData>();
	        ItemData datarow;
	        for(int i=0; i < cursor.getCount(); i++)
	        {
	        	datarow = new ItemData(cursor_item.getString(0),cursor_item.getInt(1),cursor_item.getString(2),cursor.getDouble(1),cursor.getDouble(2));
	        	Item_arr.add(datarow);
	        	cursor.moveToNext();
	        }
	        cursor_item.close();
	        cursor.close();
	        I_adapter = new ItemAdapter(this, R.layout.note_add_row, Item_arr);
	        lv_item.setAdapter(I_adapter);
        }
        
        bt_save.setOnClickListener(this);
	}
	
	public void Setting(final Context context, View v) {
    	bt_save = (Button)v.findViewById(R.id.bt_save);
        tv_title = (TextView)v.findViewById(R.id.tv_title);
        tv_group = (TextView)v.findViewById(R.id.tv_group);
        lv_item = (ListView)v.findViewById(R.id.lv_item); 
        
        qWhere = HopeNoteDb.COL_GROUP + " = '" + str_group + "'";
    	cursor = sdb.query(HopeNoteDb.TB_ITEM, new String[] {HopeNoteDb.COL_ITEM,HopeNoteDb.COL_CNT,HopeNoteDb.COL_UNIT}, qWhere, null, null, null, null);
    	
    	cursor.moveToFirst();
        Item_arr = new ArrayList<ItemData>();
        ItemData datarow;
        for(int i=0; i < cursor.getCount(); i++)
        {
        	datarow = new ItemData(cursor.getString(0),cursor.getInt(1),cursor.getString(2),null,null);
        	Item_arr.add(datarow);
        	cursor.moveToNext();
        }
        cursor.close();
        //spin_item.setPrompt("항목 선택");
        I_adapter = new ItemAdapter(this, R.layout.note_add_row, Item_arr);
        //spin_item.setAdapter(adapter);
        
        bt_save.setOnClickListener(this);
    }
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub		
		//super.onBackPressed();
		Note_Input_Tab parent = ((Note_Input_Tab)getParent());
		parent.back();
	}
	
	//항목 클래스
	static class ItemData{
		String item_name;
		int document_count;
		String item_unit;
		Number document1;
		Number document2;
		ItemData(String _item_name, int _document_count, String _item_unit, Number _document1, Number _document2){
			item_name = _item_name;
			document_count = _document_count;
			item_unit = _item_unit;
			document1 = _document1;
			document2 = _document2;
		}
	}
		
	//항목 어댑터 클래스
	static class ItemAdapter extends BaseAdapter{
		Context context;
		int layoutId;
		ArrayList<ItemData> ResultDataArr;
		LayoutInflater Inflater;
		ItemAdapter(Context _context, int _layoutId, ArrayList<ItemData> _ResultDataArr){
			context = _context;
			layoutId = _layoutId;
			ResultDataArr = _ResultDataArr;
			Inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		public int getCount(){
			return ResultDataArr.size();
		}
		
		public String getItem(int position){
			return ResultDataArr.get(position).item_name;
		}
		
		public long getItemId(int position)
		{
			return position;
		}
		
		public View getView(int position, View convertView, ViewGroup parent){
			final int pos = position;
			if(convertView == null){
				convertView = Inflater.inflate(layoutId, parent,false);
			}
			
			TextView tv_item = (TextView)convertView.findViewById(R.id.tv_item);
			tv_item.setText(ResultDataArr.get(position).item_name);
			EditText et_document1 = (EditText)convertView.findViewById(R.id.et_document1);
			EditText et_document2 = (EditText)convertView.findViewById(R.id.et_document2);
			TextView tv_unit = (TextView)convertView.findViewById(R.id.tv_unit);
			tv_item.setText(ResultDataArr.get(position).item_unit);
			if(ResultDataArr.get(position).document_count == 1)
			{
				et_document2.setVisibility(View.INVISIBLE);
			}
			return convertView;
		}
	}
	
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId() == R.id.bt_save )
		{
			ContentValues row = new ContentValues();
			//row.put(HopeNoteDb.COL_DOCUMENT1, et_value.getText().toString());
			//기록 수정
			if(str_date != null)
			{
				if(sdb.update(HopeNoteDb.TB_NOTE, row, qWhere, null) > 0)
				{
					Toast.makeText(Note_Add.this, "수정됨", Toast.LENGTH_SHORT).show();
					
					//return to Note_Search
					//intent.putExtra(HopeNoteDb.EXTRA_SEARCH, str_group);
					//setResult(RESULT_OK, intent);
					//finish();
				}
				else
				{
					Toast.makeText(Note_Add.this, "Error", Toast.LENGTH_SHORT).show();
				}
			}
			//기록 등록
			else
			{
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = new Date();
				row.put(HopeNoteDb.COL_DATE, dateFormat.format(date));
				//row.put(HopeNoteDb.COL_USER, getString(R.string.user_name));
				row.put(HopeNoteDb.COL_USER, HopeNoteDb.CURRENT_USER);
				//row.put(HopeNoteDb.COL_ITEM, spin_item.getSelectedItem().toString());
				if(sdb.insert(HopeNoteDb.TB_NOTE, null, row) > 0)
				{
					Toast.makeText(Note_Add.this, "등록 완료", Toast.LENGTH_SHORT).show();
					//et_value.setText("");
				}
				else
				{
					Toast.makeText(Note_Add.this, "등록 실패", Toast.LENGTH_SHORT).show();
				}
			}
		}
	}
}
